﻿(function () {

    var rtype = getRtype();

    switch (rtype) {
        case 'movie':
            movie(rtype);
            break;
        case 'erjitv':
            erjitv(rtype);
            break;
        case 'book':
            book(rtype);
            break;
        case 'zongyi':
            zongyi(rtype);
            break;
    }

    function movie(rtype) {
        var key = 'ptitle', container = document.getElementById('content');
        if (!container) {
            return;
        }

        on(container, 'mousedown', function (e) {
            e = e || window.event;
            var target = e.target ? e.target : e.srcElement;
            var url = '', title = '';
            if (target.tagName.toLowerCase() != 'a') {
                target = target.parentNode;
            }
            if (target.tagName.toLowerCase() == 'a') {
                url = target.href;
                title = target.parentNode.getAttribute(key);
                if (!title) {
                    title = target.parentNode.parentNode.getAttribute(key);
                }
                if (title) {
                    collect(rtype, title, url);
                }
            }

        });
    }
    function erjitv(rtype) {
        var key = 'otitle', container = document.getElementById('tv-datas');

        if (!container) { return; }
        on(container, 'mousedown', function (e) {

            e = e || window.event;
            var target = e.target ? e.target : e.srcElement;
            var url = '', title = '';
            if (target.tagName.toLowerCase() == 'a') {
                title = target.title;
                url = target.href;
                collect('tv', title, url);
                return;
            }
            target = target.parentNode;


            if (target.tagName.toLowerCase() == 'a') {
                url = target.href;
                title = target.parentNode.parentNode.getAttribute(key);

                if (title) {
                    collect('tv', title, url);
                }
            }

        });
    }
    function book(rtype) {
        var container = document.getElementById('data_list');
        if (!container) {
            return;
        }
        on(container, 'mousedown', function (e) {
            e = e || window.event;
            var target = e.target ? e.target : e.srcElement;
            var url = '', title = '';
            if (target.tagName.toLowerCase() != 'a') {
                target = target.parentNode;
            }
            if (target.tagName.toLowerCase() == 'a') {
                url = target.href;
                title = target.title;
                if (title) {
                    collect(rtype, title, url);
                }
            }
        });
    }
    function zongyi(rtype) {
        var key = 'ptitle', container = document.getElementById('zy-body');
        if (!container) {
            return;
        }
        on(container, 'mousedown', function (e) {
            e = e || window.event;
            var target = e.target ? e.target : e.srcElement;
            var url = '', title = '';

            if (target.tagName.toLowerCase() == 'a') {
                title = target.title;
                url = target.href;
                collect(rtype, title, url);
                return;
            }
            target = target.parentNode;
            if (target.tagName.toLowerCase() == 'a') {
                url = target.href;
                title = target.parentNode.getAttribute(key);
                if (title) {
                    collect(rtype, title, url);
                }
            }

        });
    }
    function collect(rtype, title, url) {
        var url = 'http://nsclick.baidu.com/h.gif',
                r = new Date().getTime(),
                parameter = {
                    pid: 113,
                    v: 'hao123-erji-res-recomand',
                    hao123_baiduid: getCookie('BAIDUID'),
                    rtype: rtype,
                    title: title,
                    url: url
                },
                key,
                obj,
                ret = [];

        key = 'collector' + r;
        window[key] = obj = new Image();
        obj.onload = obj.onerror = function () {
            window[key] = null;
        }
        parameter.r = r;
        for (key in parameter) ret.push(encodeURIComponent(key) + "=" + encodeURIComponent(parameter[key]));
        obj.src = url + '?' + ret.join('&');

    }
    function getCookie(name) {
        var reg = new RegExp("(?:; )?" + name + "=([^;]*);?");
        if (reg.test(document.cookie)) {
            return decodeURIComponent(RegExp["$1"]);
        }
        else {
            return null;
        }
    }
    function getRtype() {
        var url = window.location.href;
        if (url.indexOf('book.hao123.com') >= 0) {
            return 'book';
        }
        else if (url.indexOf('v.hao123.com/movie') >= 0) {
            return 'movie';
        }
        else if (url.indexOf('v.hao123.com/tv') >= 0) {
            return 'erjitv';
        }
        else if (url.indexOf('zongyi.hao123.com') >= 0) {
            return 'zongyi';
        }
    }
    function on(elem, type, fun) {
        if (document.addEventListener) {
            elem.addEventListener(type, fun, false);
        }
        else {
            elem.attachEvent("on" + type, fun);
        }
    }
})();